import type { ChannelData as ChannelData, ChannelItem, ServiceRes } from '@/types'
import axios from 'axios'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
// 调用函数获取 store
export const useCounterStore = defineStore('counter', () => {
  // ref -> state
  const count = ref(0)

  // computed -> getters
  const doubleCount = computed(() => count.value * 2)

  // function -> actions
  const addCount = (val: number) => {
    count.value += val
  }

  // 可以把获取频道列表的业务，整合到 store 中
  const channelList = ref<ChannelItem[]>([])
  const getChannelList = async () => {
    const res = await axios.get<ServiceRes<ChannelData>>('https://geek.itheima.net/v1_0/channels')
    channelList.value = res.data.data.channels
  }

  // 返回值
  return {count,doubleCount,addCount,channelList,getChannelList}
})
